/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <cstdlib>

#include <QtCore>
#include <QApplication>
#include <QTranslator>
#include <QLocale>

#include "ia_main_window.h"

int main(int argc, char ** argv)
{
    QApplication app(argc, argv);
    QSettings settings("STGSoft", "InetAccess");

    QString localeName(settings.value("localeName", "default").toString());
    QLocale locale;

    if (localeName == "" || localeName == "default") {
        QString lc_messages(getenv("LC_MESSAGES"));
        QString lang(getenv("LANG"));
        QString lc_all(getenv("LC_ALL"));

        qDebug() << "LC_ALL = '" << lc_all << "'";
        qDebug() << "LC_LANG = '" << lang << "'";
        qDebug() << "LC_MESSAGES = '" << lc_messages << "'";

        if (!lc_all.isEmpty() && lc_all != "\"\"") {
            localeName = QLocale(lc_all).name();
        } else if (!lc_messages.isEmpty() && lc_messages != "\"\"") {
            localeName = QLocale(lc_messages).name();
        } else if (!lang.isEmpty() && lang != "\"\"") {
            localeName = QLocale(lang).name();
        } else {
            localeName = QLocale::system().name();
        }
    }

    qDebug() << "Locale name: " << localeName; 

    QTranslator translator;
    QTranslator qtTranslator;

    translator.load("qia_" + localeName, ":/translations");
    qtTranslator.load("qt_" + localeName,
            QLibraryInfo::location(QLibraryInfo::TranslationsPath));

    app.installTranslator(&translator);
    app.installTranslator(&qtTranslator);

    app.setQuitOnLastWindowClosed(false);

    IAMainWindow win;

    if (settings.value("autoconnect", "0").toBool()) {
        win.hide();
    } else {
        win.show();
    }

    return app.exec();
}

